import { Component, OnInit } from '@angular/core';
import { AxiosService } from '../../axios.service';

@Component({
  selector: 'app-permissions-userinfo',
  templateUrl: './permissions-userinfo.component.html',
  styleUrls: ['./permissions-userinfo.component.scss']
})
export class PermissionsUserinfoComponent implements OnInit {
  isVisible = false;
  // 所有员工信息
  public userdata = [];
  // input双向绑定
  public job_no = '';
  public name = '';
  public name_PY = '';
  public department_abb = '';
  public department = '';
  public role = '';
  public level: any;
  public remark = [];
  public mobile = '';
  public mail = '';
  public password = '';
  public wechat = '';
  roleValue = '';
  remarkUpdate = [];
  levelUpdate = [];
  levelChooseVal = [];


  // 判断工号是input显示还是别的显示
  showInput: boolean = false;

  // 职位数组 remark
  public job = [
    { label: '部門主管', value: '部門主管', checked: false },
    { label: '電控工程師', value: '電控工程師', checked: false },
    { label: '機構工程師', value: '機構工程師', checked: false },
    { label: '編程工程師', value: '編程工程師', checked: false },
    { label: '治具加工員', value: '治具加工員', checked: false },
    { label: '質檢員', value: '質檢員', checked: false },
    { label: '組裝員', value: '組裝員', checked: false },
    { label: '庫房管理員', value: '庫房管理員', checked: false },
  ];
  // 賬戶權限数组 role
  // public permissions = [
  //   { 'title': '管理員', 'checked': false, 'select': 0 },
  //   { 'title': '用戶', 'checked': false, 'select': 1 },
  //   { 'title': '客戶', 'checked': false, 'select': 2 },
  // ];

  // 访问权限数组 level
  public access = [
    { label: '主管審核', value: '主管審核', checked: false },
    { label: '治具設計', value: '治具設計', checked: false },
    { label: '治具編程', value: '治具編程', checked: false },
    { label: '治具加工', value: '治具加工', checked: false },
    { label: '品質檢驗', value: '品質檢驗', checked: false },
    { label: '二次加工', value: '二次加工', checked: false },
    { label: '治具組裝', value: '治具組裝', checked: false },
    { label: '治具總檢', value: '治具總檢', checked: false },
    { label: '成品入庫', value: '成品入庫', checked: false },
    { label: '派件管理', value: '派件管理', checked: false },
    { label: '庫房管理', value: '庫房管理', checked: false },
    { label: '申請領料', value: '申請領料', checked: false },
    { label: '看板管理', value: '看板管理', checked: false },
    { label: '報表下載', value: '報表下載', checked: false },
    { label: '自動排班', value: '自動排班', checked: false },
    { label: '系統設置', value: '系統設置', checked: false },
  ];

  constructor(
    public axios: AxiosService,
  ) { }

  ngOnInit(): void {
    // 显示所有员工信息
    this.allUser();
    this.showInput = true;

  }


  // 获取所有员工信息
  allUser() {
    this.isVisible = false;
    this.axios.axiosPost('QueryUser')
      .then((data) => {
        if (data) {
          console.log(data);
          this.userdata = data['order'];
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }

  // 编辑 表格中的数据绑定到input中
  edit(item) {
    this.showInput = false;
    console.log(item);
    this.name = item.name;
    this.name_PY = item.name_PY;
    this.job_no = item.job_no;
    this.department = item.department;
    this.department_abb = item.department_abb;
    this.roleValue = item.role;
    // this.level = item.level;
    this.mail = item.mail;
    this.mobile = item.mobile;
    this.chooseRole(this.roleValue);
    this.chooseRemark(this.remarkUpdate);

    //将所有的职位改为false
    this.job.forEach(item => {
      item.checked = false;
    });

    // 将所有的访问权限改为false
    this.access.forEach(item => {
      item.checked = false;
    });

    //处理remark
    let remarkArr = item.remark.split(',');
    for (let i = 0; i < remarkArr.length; i++) {
      for (let j = 0; j < this.job.length; j++) {
        if (remarkArr[i] === this.job[j].label) {
          this.job[j].checked = true;
        }
      }
    }
    this.remark = remarkArr;
    console.log(this.job)
    // console.log(item.remark)

    // 处理level
    // 处理item.level
    // debugger
    let item_level_two = item.level
    item_level_two = item.level.toString(2);
    console.log(item.level);
    let levelArr = item_level_two.split('');
    console.log(levelArr)
    const levelLength = levelArr.length;
    const accessLength = this.access.length;
    for (let i = levelLength - 1; i >= 0; i--) {
      if (levelArr[i] === '1') {
        this.access[accessLength - levelLength + i].checked = true;
      } else {
        this.access[accessLength - levelLength + i].checked = false;
      }
    }
    this.level = levelArr;
    console.log('@@ \n', this.access)
  }


  // 清空按钮 解除disabled 清空input框
  delInput() {
    this.showInput = true;
    this.name = '';
    this.name_PY = '';
    this.job_no = '';
    this.department = '';
    this.department_abb = '';
    this.roleValue = '';
    this.level = [];
    this.remark = [];
    this.mail = '';
    this.mobile = '';
    this.password = '';
    this.job.forEach(item => {
      item.checked = false;
    });
    this.access.forEach(item => {
      item.checked = false;
    });
  }

  // 选择职位
  chooseRemark(data: any) {
    let remarkChoose = data.filter((item) => {
      return item.checked === true;
    })
    this.remarkUpdate = [];
    for (let i = 0; i < remarkChoose.length; i++) {
      this.remarkUpdate.push(remarkChoose[i].label);
    }
    this.remark = this.unique(this.remarkUpdate);
  }

  // 选择访问权限
  chooseLevel(data) {
    console.log('111\n', data);
    this.levelChooseVal = data;
  }

  // 选择账户权限
  chooseRole(value) {
    console.log(value)
    this.role = value;
  }
  // 保存新添加的信息
  save() {
    // 处理level的checkbox
    console.log(this.levelChooseVal)
    const tempArr = [];
    for (let i = 0; i < this.levelChooseVal.length; i++) {
      if (this.levelChooseVal[i].checked) {
        tempArr.push(1);
      } else {
        tempArr.push(0)

      }
    }
    console.log('输出结果  = \n', tempArr)
    const str = tempArr.toString()
    const num = str.replace(/,/g, "");
    console.log('输出结果  = \n', num)
    const ten = parseInt(num, 2)
    console.log('输出结果  = \n', ten)
    this.level = ten
    console.log('输出结果  = \n', this.level)

    // return
    console.log(this.password)
    let job_number = [];
    for (let i = 0; i < this.userdata.length; i++) {
      job_number.push(this.userdata[i].job_no)
    }
    if (this.job_no !== '' && this.name !== '' && this.name_PY !== '' && this.password !== '') {
      if (job_number.includes(this.job_no)) {
        console.log('更新')
        this.axios.axiosPost('UpdateUser', {
          'name': this.name,
          'name_PY': this.name_PY,
          'job_no': this.job_no,
          'department': this.department,
          'department_abb': this.department_abb,
          'role': this.role,
          'level': this.level,
          'password': this.password,
          'remark': this.remark,
          'wechat': this.wechat,
          'mail': this.mail,
          'mobile': this.mobile
        })
          .then((data) => {
            if (data['result'] === 'ok') {
              this.allUser();
            }

          })
        console.log(this.password)
      } else {
        // debugger
        console.log('增加')
        this.axios.axiosPost('AddUser', {
          'name': this.name,
          'name_PY': this.name_PY,
          'job_no': this.job_no,
          'department': this.department,
          'department_abb': this.department_abb,
          'role': this.role,
          'level': this.level,
          'password': this.password,
          'remark': this.remark,
          'wechat': this.wechat,
          'mail': this.mail,
          'mobile': this.mobile
        })
          .then((data) => {
            if (data['result'] === 'ok') {
              this.allUser();
            }
          })
      }
      this.name = '';
      this.name_PY = '';
      this.job_no = '';
      this.department = '';
      this.department_abb = '';
      this.roleValue = '';
      this.level = [];
      this.remark = [];
      this.mail = '';
      this.mobile = '';
      this.password = '';
      this.job.forEach(item => {
        item.checked = false;
      });
      this.access.forEach(item => {
        item.checked = false;
      });
    } else {
      alert("工號、用戶名、英文名、密碼不能為空")
    }

  }

  //  删除员工信息
  handleOk() {
    this.axios.axiosPost('DeleteUser', { 'job_no': this.job_no })
      .then((data) => {
        console.log(data)
        if (data['result'] === 'ok') {
          this.allUser();
        }
      })
  }
  showModal(): void {
    this.isVisible = true;
  }
  handleCancel(): void {
    console.log('Button cancel clicked!');
    this.isVisible = false;
  }

  // 去重
  unique(arr: any) {
    var array = [];
    for (var i = 0; i < arr.length; i++) {
      if (array.indexOf(arr[i]) === -1) {
        array.push(arr[i])
      }
    }
    return array;
  }
}


